<script>
export default {
  data() {
    return {
      form: {},
      id:JSON.parse(localStorage.getItem("user")).id
    }
  },
  created() {
    this.getMyInfo()
  },
  methods: {
    submit() {
      this.$confirm('您确定要修改吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.post('/user/update',this.form)
            .then(res => {
              if (res.code===200) {
                this.$message.success(res.message);
                this.$store.dispatch('updateInfo', {user: this.form});
              }
            })
      })
    },
    getMyInfo(){
      this.request.get('/user/findById/'+this.id)
          .then(res=>{
            this.form=res.user
          })
    },
    handleAvatarSuccess(res){
      this.form.avatar=res.msg
    }
  }
}
</script>

<template>
  <div class="animate__animated animate__fadeInLeft" style="display: flex; justify-content: center; align-items: center; height: 80vh;">
    <el-card style="width: 45%;">
      <el-form label-width="100px">
        <el-form-item  prop="questionHover">
          <div class="avatar-container">
            <el-upload
                action="http://localhost:8099/api/file/upload"
                class="avatar-uploader"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
            >
              <img style="width: 100px; height: 100px; border-radius: 50%; margin-left: 180px" v-if="form.avatar" :src="form.avatar" class="avatar" alt="">

              <el-button v-else size="small" v-if="form.avatar===null" type="primary">上传头像</el-button>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item style="display: none;">
          <el-input v-model="form.id" type="hidden"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input disabled v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" style="margin-left: 200px" @click="submit">提交</el-button>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>

<style scoped>

</style>